import { _decorator, Component, Label, Node, v3 } from 'cc';
import { EventManager } from '../event/EventManager';
import { UIManager } from './UIManager';
import { PrefabEnum } from './PrefabEnum';
const { ccclass, property } = _decorator;

@ccclass('Modal')
export class Modal extends Component {
    @property(Node)
    mask:Node
    @property(Label)
    title:Label
    @property(Label)
    content:Label
    @property(Node)
    cancel:Node
    @property(Node)
    confirm:Node

    start() {
       
        this.cancel.on(Node.EventType.TOUCH_END, this.onCancel, this)
        this.confirm.on(Node.EventType.TOUCH_END, this.onConfirm, this)
    }

    show(title:string, content:string, isShowCancel:boolean = true) {
        this.title.string = title
        this.content.string = content
        if(!isShowCancel){
            this.cancel.active = false

            let pos = this.confirm.position
            this.confirm.position = v3(0, pos.y, pos.z)
        }
    }

    showComfrim(obj:{title:string, content:string, isShowCancel?:boolean, cancelText?:string, confrimText?:string}){
        this.show(obj.title, obj.content, obj.isShowCancel)
        if(obj.cancelText){
            this.cancel.getChildByName('Label').getComponent(Label).string = obj.cancelText
        }
        if(obj.confrimText){
            this.confirm.getChildByName('Label').getComponent(Label).string = obj.confrimText
        }
    }

    onCancel(){
        EventManager.instance.emit(EventManager.EventType.ModalConfirm, false)
        UIManager.instance.closeUI(PrefabEnum.Modal)
    }

    onConfirm(){
        EventManager.instance.emit(EventManager.EventType.ModalConfirm, true)
        UIManager.instance.closeUI(PrefabEnum.Modal)
    }
}


